package io.gbznt.modules.buss.quartz;
 
import java.util.List;

import io.gbznt.config.TcpStartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import io.gbznt.modules.buss.entity.BussInspectionPlanEntity;
import io.gbznt.modules.buss.service.BussInspectionPlanService;
 
/**
 * Created by pangkunkun on 2017/9/3.
 */
@Component
@Order(value = 0)
public class MyApplicationRunner implements ApplicationRunner {

	private static Logger logger = LoggerFactory.getLogger(MyApplicationRunner.class);

	@Autowired
	private BussInspectionPlanService bussInspectionPlanService;
	
	 public static String JOB_GROUP_NAME = "XLXXCC_JOB_GROUP";  
	 public static String TRIGGER_GROUP_NAME = "XLXXCC_JOB_GROUP"; 
	
    @Override
    public void run(ApplicationArguments var1) throws Exception{
		logger.info("启动巡检计划初始化");
        List<BussInspectionPlanEntity> n =  bussInspectionPlanService.list(null);
        for (int i = 0; i < n.size(); i++) {
        	BussInspectionPlanEntity plan = new BussInspectionPlanEntity();
        	plan = n.get(i);
        	try {  
        		if(plan.getStatus() != null){
        	
	             	if(plan.getStatus() == 1){
	                    System.out.println("【系统启动】...");    
	                    String cron = "";
	                    if(plan.getFrequencyFormulatype().equals("分")){
	                    	 cron = "0 */"+plan.getFrequencyFormula()+" * * * ?";
	                    }else if(plan.getFrequencyFormulatype().equals("时")){
	                    	 cron = "* * 0-"+plan.getFrequencyFormula()+" * * ?";
	                    }else if(plan.getFrequencyFormulatype().equals("天")){
	                    	 cron = "* * 0 "+plan.getFrequencyFormula()+" * ?";
	                    }else if(plan.getFrequencyFormulatype().equals("周")){
	                    	 cron = "0 0 0 ? * "+plan.getFrequencyFormula()+" ";
	                    }
	                   // String crop = "0/"+plan.getFrequencyFormula()+" * * * * ?";
	                    //jobName 任务名  reigname 触发器名称都设置为计划名称
	                    DynamicTask.addJob(plan.getId(), JOB_GROUP_NAME, plan.getId(), TRIGGER_GROUP_NAME, MyJob.class, cron,plan);    
	            		
	             	}
        		
        		}
            } catch (Exception e) {  
                e.printStackTrace();  
            } 
		}
        
        //第二个定时器
      //  DynamicTask.addJobnot("weather", JOB_GROUP_NAME1, "weather", TRIGGER_GROUP_NAME1, MyJobweather.class, "0/10 * * * * ?");

        
    }
    
    
    public static String JOB_GROUP_NAME1 = "WEATHER_JOB_GROUP";  
	 public static String TRIGGER_GROUP_NAME1 = "WEATHER_JOB_GROUP"; 
	
  /* @Override
   public void run2(ApplicationArguments var1) throws Exception{
       System.out.println("erather定时器启动");
       
       //第二个定时器
       DynamicTask.addJobnot("weather", JOB_GROUP_NAME1, "weather", TRIGGER_GROUP_NAME1, MyJobweather.class, "0/10 * * * * ?");
   }*/
}
